//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package common;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

public class RoadsideShopSlot {
  public static final int Advertise = 1;
  public static final int Sold = 2;
  private int inventoryId;
  private int price;
  private int inventoryCount;
  private int actionMask;
  private LogicLong buyerId;
  private final int slotIndex;

  public RoadsideShopSlot(int index) {
    this.slotIndex = index;
    this.clear();
  }

  public RoadsideShopSlot(RoadsideShopSlot s) {
    this.slotIndex = s.index();
    this.update(s.inventoryId(), s.price(), s.count(), s.isAdvertise());
  }

  public void decode(ByteStream stream) {
    this.inventoryId = stream.readInt();
    this.price = stream.readInt();
    this.inventoryCount = stream.readInt();
    this.actionMask = stream.readByte() & 3;
    if((this.actionMask & 2) != 0) {
      this.buyerId = stream.readLong();
    }

  }

  public void update(int oid, int cost, int count, boolean advertise) {
    this.inventoryId = oid;
    this.price = cost;
    this.inventoryCount = count;
    if(advertise) {
      this.actionMask = 1;
    } else {
      this.actionMask = 0;
    }

  }

  public void clear() {
    this.inventoryId = 0;
    this.price = 0;
    this.inventoryCount = 0;
    this.actionMask = 0;
    this.buyerId = new LogicLong(0L);
  }

  public int index() {
    return this.slotIndex;
  }

  public boolean isSlotEmpty() {
    return this.inventoryId == 0;
  }

  public int inventoryId() {
    return this.inventoryId;
  }

  public int price() {
    return this.price;
  }

  public int count() {
    return this.inventoryCount;
  }

  public long buyer() {
    return this.buyerId.longValue();
  }

  public int actionMask() {
    return this.actionMask;
  }

  public void setSold() {
    this.actionMask |= 2;
  }

  public void setAdvertise() {
    this.actionMask |= 1;
  }

  public boolean isSold() {
    return (this.actionMask & 2) != 0;
  }

  public boolean isAdvertise() {
    return (this.actionMask & 1) != 0;
  }

  public void getToJsonObject(JsonObject jSlot) {
    jSlot.addProperty("Slot", Integer.valueOf(this.slotIndex));
    JsonArray jFlags = new JsonArray();
    if(!this.isSlotEmpty()) {
      jSlot.addProperty("ID", Integer.valueOf(this.inventoryId));
      jSlot.addProperty("Price", Integer.valueOf(this.price));
      jSlot.addProperty("Amount", Integer.valueOf(this.inventoryCount));
      if(this.isAdvertise()) {
        jFlags.add(new JsonPrimitive("advertise"));
      }

      if(this.isSold()) {
        jFlags.add(new JsonPrimitive("sold"));
      }

      jSlot.add("Flags", jFlags);
    }

  }

  public JsonObject getAsJsonObject() {
    JsonObject jSlot = new JsonObject();
    this.getToJsonObject(jSlot);
    return jSlot;
  }

  public String toString() {
    String val = String.format("[ Slot: %d: ", new Object[]{Integer.valueOf(this.slotIndex)});
    if(this.isSlotEmpty()) {
      val = val + "empty ]";
    } else {
      val = val + "\"" + this.inventoryId + "\"" + " flags: ";
      if(this.isAdvertise()) {
        val = val + "advertise ";
      }

      if(this.isSold()) {
        val = val + "sold ";
      }

      if((this.actionMask & 3) == 0) {
        val = val + "empty ";
      }

      val = val + ", Price: " + this.price;
      val = val + ", Amount: " + this.inventoryCount;
      val = val + " ]";
    }

    return val;
  }
}
